import { Injectable } from '@angular/core';
import { RestService } from '@abp/ng.core';
import { CreateUpdateInventoryTransferDto, InventoryTransferDto, InventoryTransferDetailDto } from './models';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class InventoryTransferService {
  apiName = 'Default';

  constructor(private restService: RestService, private http: HttpClient) {}

  getList(params: any = {}): Observable<any> {
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/inventory-transfer',
      params
    });
  }

  searchByTransferNo(transferNoKeyword: string, params: any = {}): Observable<any> {
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/inventory-transfer/search-by-transfer-no',
      params: {
        transferNoKeyword,
        skipCount: params['skipCount'] || 0,
        maxResultCount: params['maxResultCount'] || 10,
        sorting: params['sorting'] || ''
      }
    });
  }

  get(id: string): Observable<InventoryTransferDto> {
    return this.restService.request<any, InventoryTransferDto>({
      method: 'GET',
      url: `/api/app/inventory-transfer/${id}`
    });
  }

  getById(id: string): Observable<InventoryTransferDto> {
    return this.restService.request<any, InventoryTransferDto>({
      method: 'GET',
      url: `/api/app/inventory-transfer/${id}`
    });
  }

  /**
   * 添加主表和明细
   */
  create(data: any) {
    return this.http.post('https://localhost:44359/api/app/inventory-transfer', data);
  }

  update(id: string, input: CreateUpdateInventoryTransferDto): Observable<InventoryTransferDto> {
    return this.restService.request<any, InventoryTransferDto>({
      method: 'PUT',
      url: `/api/app/inventory-transfer/${id}`,
      body: input
    });
  }

  delete(id: string): Observable<void> {
    return this.restService.request<any, void>({
      method: 'DELETE',
      url: `/api/app/inventory-transfer/${id}`
    });
  }

  /**
   * 批量新增明细
   */
  batchCreate(details: InventoryTransferDetailDto[]): Observable<any> {
    return this.http.post('/api/app/merchandise/batch-create', details);
  }

  /**
   * 批量更新明细
   */
  batchUpdate(details: InventoryTransferDetailDto[]): Observable<any> {
    return this.http.post('/api/app/merchandise/batch-update', details);
  }

  /**
   * 删除单条明细
   */
  deleteDetail(id: string) {
    return this.http.delete(`/api/app/merchandise/${id}`);
  }
} 